package com.bianqian.demo.controller;

import com.bianqian.demo.dto.*;
import com.bianqian.demo.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 页面兼容性控制器 - 支持前端原有的API路径
 */
@RestController
@RequestMapping("/api/pages")
public class PagesController {
    
    @Autowired
    private NoteService noteService;
    
    /**
     * 获取笔记列表 - 兼容性路由
     */
    @GetMapping("/list")
    public ApiResponse<NoteListResponse> getNotesList(
            HttpServletRequest request,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "pinned", required = false) Boolean pinned) {
        
        String token = getTokenFromRequest(request);
        return noteService.getNotesList(token, page, pageSize, keyword, category, pinned);
    }
    
    /**
     * 创建笔记 - 兼容性路由
     */
    @PostMapping("/create")
    public ApiResponse<NoteDto> createNote(@Valid @RequestBody CreateNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.createNote(token, request);
    }
    
    /**
     * 创建笔记 - 兼容性路由 (notes/create)
     */
    @PostMapping("/notes/create")
    public ApiResponse<NoteDto> createNoteCompatibility(@Valid @RequestBody CreateNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.createNote(token, request);
    }
    
    /**
     * 更新笔记 - 兼容性路由
     */
    @PutMapping("/update")
    public ApiResponse<NoteDto> updateNote(@Valid @RequestBody UpdateNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.updateNote(token, request);
    }
    
    /**
     * 删除笔记 - 兼容性路由
     */
    @DeleteMapping("/notes/delete")
    public ApiResponse<Void> deleteNote(@Valid @RequestBody DeleteNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.deleteNote(token, request);
    }
    
    /**
     * 置顶/取消置顶笔记 - 兼容性路由
     */
    @PutMapping("/notes/pin")
    public ApiResponse<NoteDto> pinNote(@Valid @RequestBody PinNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.pinNote(token, request);
    }
    
    /**
     * 加密笔记 - 兼容性路由
     */
    @PutMapping("/notes/encrypt")
    public ApiResponse<NoteDto> encryptNote(@Valid @RequestBody EncryptNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.encryptNote(token, request);
    }
    
    /**
     * 解密笔记 - 兼容性路由
     */
    @PutMapping("/notes/decrypt")
    public ApiResponse<NoteDto> decryptNote(@Valid @RequestBody DecryptNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.decryptNote(token, request);
    }
    
    /**
     * 分类笔记 - 兼容性路由
     */
    @PutMapping("/notes/categorize")
    public ApiResponse<NoteDto> categorizeNote(@Valid @RequestBody CategorizeNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.categorizeNote(token, request);
    }
    
    /**
     * 批量分类笔记
     */
    @PutMapping("/notes/batch-categorize")
    public ApiResponse<BatchCategorizeResponse> batchCategorizeNotes(@Valid @RequestBody BatchCategorizeRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.batchCategorizeNotes(token, request);
    }
    
    /**
     * 复制笔记 - 兼容性路由
     */
    @PostMapping("/notes/copy")
    public ApiResponse<NoteDto> copyNote(@Valid @RequestBody CopyNoteRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.copyNote(token, request);
    }
    
    /**
     * 同步笔记 - 兼容性路由
     */
    @PostMapping("/notes/sync")
    public ApiResponse<SyncResponse> syncNotes(@Valid @RequestBody SyncNotesRequest request, HttpServletRequest httpRequest) {
        String token = getTokenFromRequest(httpRequest);
        return noteService.syncNotes(token, request);
    }
    
    /**
     * 测试接口 - 用于检查服务是否正常
     */
    @GetMapping("/test")
    public ApiResponse<String> test() {
        return ApiResponse.success("服务正常", "OK");
    }
    
    /**
     * 测试复制接口路由
     */
    @GetMapping("/notes/copy/test")
    public ApiResponse<String> testCopyRoute() {
        return ApiResponse.success("复制接口路由正常", "OK");
    }
    
    /**
     * 从请求头中获取token
     */
    private String getTokenFromRequest(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            return authHeader.substring(7);
        }
        return null;
    }
}
